<template>
  <div class="flex flex-col h-full">
    <a-card>
      <div class="flex justify-between items-center">
        <a-button type="primary" class="mr-2" @click="router.go(-1)"> 返回 </a-button>
        <BaseExport type="物资" name="月度成本核算明细" code="00001166" :search-params="{ lsh: route.query?.lsh }">导出</BaseExport>
        <a-descriptions class="mt-3 ml-5" :column="4">
          <a-descriptions-item label="核算月度"> {{ formState.month || '-' }}</a-descriptions-item>
          <a-descriptions-item label="核算人">{{ formState.hsr || empName || '-' }}</a-descriptions-item>
          <a-descriptions-item label="核算时间"> {{ formState.hssj || '-' }}</a-descriptions-item>
        </a-descriptions>
      </div>
    </a-card>
    <a-card class="mt-2 flex-1">
      <div class="flex justify-end mb-2">
        <a-button @click="onClickPrint">打印</a-button>
      </div>
      <BaseTable :data-source="tableData" column-code="00001166" :pagination="pagination" :loading="loading" :extra-height="60" fill row-key="sfxmbBh">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'ypmc'">
            <MedicineInfo :value="{ mc: record.ypmc, gg: record.gg, cj: record.cj }" />
          </template>
        </template>
        <template #summary>
          <a-table-summary fixed>
            <a-table-summary-row>
              <a-table-summary-cell>合计</a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ total.cbjQc }}</div>
                </a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ total.slQc }}</div>
                </a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ total.jeQc }}</div>
                </a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ total.cbjHs }}</div>
                </a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ total.slSr }}</div>
                </a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ total.jeSr }}</div>
                </a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ total.slFc }}</div>
                </a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ total.jeFc }}</div>
                </a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ total.slQm }}</div>
                </a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ total.jeQm }}</div>
                </a-typography-text>
              </a-table-summary-cell>
            </a-table-summary-row>
          </a-table-summary>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref, watchEffect } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import BaseTable from '@/components/BaseTable'
import { post } from '@/utils/request'

import useUserStore from '@/store/user'

const formState = ref({})
const route = useRoute()
const router = useRouter()

const { empName } = useUserStore()
const total = ref({})
// getMonthWeightedAverageDetailPageList
const { loading, tableData, getData, pagination } = useTable({
  url: '/oos/comprehenive/getMonthWeightedAverageDetailPageList',
  params: {
    lsh: route.query?.lsh
  }
})

const getMainData = async () => {
  const { data, code } = await post('/oos/comprehenive/getMonthWeightedAverageList', {
    lsh: route.query?.lsh
  })
  if (code === 0) {
    formState.value = data[0]
  }
  const res = await post('/oos/comprehenive/getMonthWeightedAverageDetailList', {
    lsh: route.query?.lsh
  })
  if (res.code === 0) {
    total.value = res.data
  }
}

// 打印
const onClickPrint = async () => {
  const { code, msg, data } = await post('/oos/comprehenive/printMtrMonthWeightedAverageDetail', { lsh: route.query?.lsh, mtrType: 0 })
  if (code !== 0) return message.error(msg || '获取打印数据失败')
  window.openExe?.rpt(data) //调用winform打印
}

watchEffect(() => {
  if (route.query?.lsh) {
    getData()
    getMainData()
  }
})
</script>
<style lang="less" scoped></style>
